package Demo04;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MyWin_Code extends JFrame implements ActionListener {
    //extends JFrame 继承窗口//implements ActionListener 窗口中的按钮、控件

    //主面板
    public  JPanel mainPanel;

    //标签
    //用户登录
    //用户名
    //密码
    public  JLabel lblLogin,lblUserName,lblUserPwd;

    //用户名框
    //密码框
    public  JTextField txtUserName;
    public  JPasswordField txtUserPwd;

    //登录按钮
    //退出按钮
    public  JButton btnLogin,btnExit;

    public  MyWin_Code(){
        setTitle("My_Win");
        this.setBounds(200,200,500,300);
        this.setBackground(Color.PINK);

        //实例化面板
        mainPanel =new JPanel(null);
        this.setContentPane(mainPanel);

        lblLogin=new JLabel("用户登录");
        //字体大小
        Font font =new Font("黑体",Font.BOLD,32);
        //定义font
        lblLogin.setFont(font);
        //字体颜色
        lblLogin.setForeground(Color.red);
        //前景色
        lblLogin.setBackground(Color.GREEN);

        lblUserName=new JLabel("用户名：",JLabel.CENTER);
        lblUserPwd=new JLabel("密码:",JLabel.CENTER);

        txtUserName=new JTextField(30);
        txtUserPwd=new JPasswordField(30);

        btnLogin=new JButton("登录");
        btnExit=new JButton("退出");





        //标签位置
        lblLogin.setBounds(180,10,200,100);
        lblUserName.setBounds(50,100,90,30);
        txtUserName.setBounds(140,100,300,30);
        lblUserPwd.setBounds(50,140,90,30);
        txtUserPwd.setBounds(140,140,300,30);
        btnLogin.setBounds(160,190,60,30);
        btnExit.setBounds(280,190,60,30);


        //按钮监听
        btnLogin.addActionListener(this);
        btnExit.addActionListener(this);


        //将标签放入面板
        mainPanel.add(lblLogin);
        mainPanel.add(lblUserName);
        mainPanel.add(lblUserPwd);
        mainPanel.add(txtUserName);
        mainPanel.add(txtUserPwd);
        mainPanel.add(btnLogin);
        mainPanel.add(btnExit);


        this.setResizable(false);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new  MyWin_Code();
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        //implements ActionListener
        //监听
        if (e.getSource().equals(btnLogin)){
            String ms="用户名:"+txtUserName.getText();
            ms+="密码:"+txtUserPwd.getPassword();
            JOptionPane.showMessageDialog(this,ms);
        }
        if (e.getSource().equals(btnExit)){
//            System.out.println();
            System.exit(0);
        }




    }
}
